package visualizer;

import java.io.InputStream;
import java.net.URL;

import java.util.*;
import java.awt.*;
import java.applet.Applet;

public class Graph extends Applet {
	
	GraphPanel panel;
	
	int carnum;
	
	Thread LightThrd[] = new Thread[3];
	
	public void init() {
		setLayout(new BorderLayout());

		panel = new GraphPanel(this);
		add("Center", panel);

		//carnum = Integer.parseInt(getParameter("carnum"));
		//carnum=Math.min(carnum, 70);
		
		/*for (int k=0; k<carnum; k++)
			panel.findNode(Integer.toString(k));
		*/
		//panel.lghtOrStp=1;
		
		/*for (int k=0; k<3; k++){
			LightThrd[k] = new Thread(panel.light[k]);
			panel.light[k].redpauss=(k+1)*1000+3000;
			panel.light[k].greenpauss=panel.light[k].redpauss;
			LightThrd[k].start();
		}
		panel.carpermin[0].time0 = System.currentTimeMillis();
		panel.carpermin[0].carnum=0;
*/
		Panel btpnl=new Panel();
		add("South", btpnl);

//		btpnl.add(new Button("Start"));
	//	btpnl.add(new Button("End"));

	//	btpnl.add(new Button("Stop Sign"));
	//	btpnl.add(new Button("Traffic Light"));
	//	btpnl.add(new Button("New Schedule for Lights"));
	}

	public boolean action(Event evt, Object arg){
		if (((Button)evt.target).getLabel().equals("Traffic Light"))
		{
			if (panel.lghtOrStp==0){
				panel.lghtOrStp=1;
				for (int k=0; k<3; k++){
					LightThrd[k] = new Thread(panel.light[k]);
					panel.light[k].redpauss=(k+1)*1000+3000;
					panel.light[k].greenpauss=panel.light[k].redpauss;
					LightThrd[k].start();
					panel.carpermin[0].time0 = System.currentTimeMillis();
					panel.carpermin[0].carnum=0;
				}
			}
		}
		else if  (((Button)evt.target).getLabel().equals("Stop Sign"))
		{
			panel.lghtOrStp=0;
			for(int k=0; k<3; k++){
				if (LightThrd[k].isAlive())
					LightThrd[k].stop();
			}
			panel.carpermin[0].time0 = System.currentTimeMillis();
			panel.carpermin[0].carnum=0;
		}
		else if  (((Button)evt.target).getLabel().equals("End"))
		{
			for(int k=0; k<3; k++){
				if (LightThrd[k].isAlive())
					LightThrd[k].stop();
			} 
			panel.stop();
		}
		else if  (((Button)evt.target).getLabel().equals("Start"))
		{
			if(panel.lghtOrStp==1)
				for(int k=0; k<3; k++){
					if (!LightThrd[k].isAlive()){
						LightThrd[k] = new Thread(panel.light[k]);
						LightThrd[k].start();
					}
				} 
			if (!panel.relaxer.isAlive())
				panel.start();
		}
		else if  (((Button)evt.target).getLabel().equals("New Schedule for Lights"))
		{
			if (panel.lghtOrStp==1){
				for(int k=0; k<3; k++){
					if (LightThrd[k].isAlive()){
						panel.light[k].redpauss=(int)(Math.random()*6000)+3000;
						panel.light[k].greenpauss=(int)(Math.random()*6000)+3000;
					}
				}
				panel.carpermin[0].time0 = System.currentTimeMillis();
				panel.carpermin[0].carnum=0;
				panel.carpermin[0].carwt=0;

			}
		}
		return true;
	}
	public void start() {
		panel.start();
	}
	public void stop() {
		panel.stop();
	}
}
